//
//  DianTaiViewController.m
//  ProJectB_1
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 yangshukai. All rights reserved.
//

#import "DianTaiViewController.h"

@interface DianTaiViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *dianTaiList;
@property (weak, nonatomic) IBOutlet UITableView *ListTable;
@property (nonatomic,strong)NSMutableArray *headListArray;
@property (nonatomic,strong)UIView *headView;
@property (nonatomic,strong)CycleScrollView *scrollView;


@end

@implementation DianTaiViewController

- (NSMutableArray *)dianTaiList{
    if (!_dianTaiList) {
        _dianTaiList = [NSMutableArray arrayWithCapacity:0];
    }
    return _dianTaiList;
}
- (NSMutableArray *)headListArray{
    if (!_headListArray) {
        _headListArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _headListArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"耳边轻语";
    [_ListTable registerNib:[UINib nibWithNibName:@"DianTaiListTableViewCell" bundle:nil] forCellReuseIdentifier:@"DianTaiListTableViewCell"];
    [self getData];
    self.ListTable.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched; // 分割线
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 230)];
    _headView.backgroundColor = [UIColor flatPumpkinColor];
    self.ListTable.tableHeaderView = _headView;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"主页" style:(UIBarButtonItemStylePlain) target:self action:@selector(Menu:)];
}

- (void)Menu:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData{
    [NetWorkRequestManager requestWithType:POST urlString:DianTaiLIST_URL parDic:@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} finish:^(NSData *data) {
        if (data == nil) {
            return ;
        }
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
            // 电台列表数据
            NSArray *allListArr = dataDic[@"data"][@"alllist"];
            for (NSDictionary *dic in allListArr) {
                DianTaiListModel *model = [[DianTaiListModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.dianTaiList addObject:model];
            }
            // 头视图滚动数据
            NSArray *headListArr = dataDic[@"data"][@"carousel"];
            for (NSDictionary *dic in headListArr) {
                HeadImage *model = [[HeadImage alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.headListArray addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                // 创建轮播图
                [self creatCycleScrollView];
                // 刷新
                [self.ListTable reloadData];
            });
 
        });
    } error:^(NSError *error) {
        NSLog(@"数据请求失败");
    }];
    
    
}
// 轮播图
- (void)creatCycleScrollView {
    _scrollView = [[CycleScrollView alloc]initWithFrame:_headView.bounds animationDuration:2.3];
    NSMutableArray *viewsArray = [@[] mutableCopy];
    for (int i = 0; i < _headListArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:_scrollView.bounds];
        imageView.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.f green:arc4random()%256 / 255.f blue:arc4random()%256 / 255.f alpha:1];
        HeadImage *model = _headListArray[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
        [viewsArray addObject:imageView];
    }
    [self.headView addSubview:_scrollView];
    _scrollView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    _scrollView.totalPagesCount = viewsArray.count;
    __weak NSArray *cycleArr = _headListArray;
    __weak DianTaiViewController *DTVC = self;
    _scrollView.TapActionBlock = ^(NSInteger pageIndex){
        NSLog(@"点击了第%ld个",(long)pageIndex);
        HeadImage *model = cycleArr[pageIndex];
        NSLog(@"url = %@", model.url);
        NSArray *cArray = [model.url componentsSeparatedByString:@"/"];
        DianTaiInfoTableViewController *infoVC = [[DianTaiInfoTableViewController alloc]init];
        infoVC.radioid = cArray.lastObject;
        [DTVC.navigationController pushViewController:infoVC animated:YES];
    };

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dianTaiList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DianTaiListTableViewCell *cell = [_ListTable dequeueReusableCellWithIdentifier:@"DianTaiListTableViewCell"];
    DianTaiListModel *model = _dianTaiList[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setDataWithModel:model];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 230;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DianTaiListModel *model = _dianTaiList[indexPath.row];
    DianTaiInfoTableViewController *VC = [[DianTaiInfoTableViewController alloc]init];
    VC.radioid = model.radioid;
    [self.navigationController pushViewController:VC animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
